package com.dragon.mmochat.model.roleDeal;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatSerial;
import com.dragon.mmochat.model.enumType.MmochatPhoneCardType;
import com.dragon.mmochat.model.enumType.MmochatRoleDealState;

//角色交易订单
public class MmochatRoleDeal implements Comparable<MmochatRoleDeal> {
	private static MmochatSerial serial = new MmochatSerial();
	private int id;// 订单号
	private int sellerId;// 卖家的联系ID
	private int sellRoleId;// 要出售的角色的角色ID
	private String sellRoleName;// 要出售的角色的名字
	private int price = 0;// 售价(单位：元)
	private String phone = null;// 卖家手机号
	private int buyerId;// 买家的联系ID
	private String buyerName;// 买家的角色名
	private long buyerSkyId;// 买家的skyId
	private MmochatRoleDealState state = MmochatRoleDealState.创建中;
	// 买家上传的充值卡信息:id-->MmochatPhoneCard
	private Map<Integer, MmochatPhoneCard> phoneCards = new ConcurrentHashMap<Integer, MmochatPhoneCard>();
	private String dealReason = "";// 处理原因
	private long createTime = System.currentTimeMillis();
	private long lifeTime = MmochatConstant.MS_PER_HOUR;// 订单生命时间

	// 联通区段
	public static String[] ChinaUnicom = new String[] { "130", "131", "132",
			"155", "156", "185", "186" };
	// 移动区段
	public static String[] ChinaMobile = new String[] { "134", "135", "136",
			"137", "138", "139", "150", "151", "152", "157", "158", "159",
			"182", "183", "187", "188" };

	public MmochatRoleDeal() {
		id = serial.addAndGetSerialId();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getSellerId() {
		return sellerId;
	}

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public int getSellRoleId() {
		return sellRoleId;
	}

	public void setSellRoleId(int sellRoleId) {
		this.sellRoleId = sellRoleId;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getBuyerId() {
		return buyerId;
	}

	public void setBuyerId(int buyerId) {
		this.buyerId = buyerId;
	}

	public MmochatRoleDealState getState() {
		return state;
	}

	public void setState(MmochatRoleDealState state) {
		this.state = state;
	}

	public Map<Integer, MmochatPhoneCard> getPhoneCards() {
		return phoneCards;
	}

	public void setPhoneCards(Map<Integer, MmochatPhoneCard> phoneCards) {
		this.phoneCards = phoneCards;
	}

	public String getDealReason() {
		return dealReason;
	}

	public void setDealReason(String dealReason) {
		this.dealReason = dealReason;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public static String[] getChinaUnicom() {
		return ChinaUnicom;
	}

	public static void setChinaUnicom(String[] chinaUnicom) {
		ChinaUnicom = chinaUnicom;
	}

	public static String[] getChinaMobile() {
		return ChinaMobile;
	}

	public static void setChinaMobile(String[] chinaMobile) {
		ChinaMobile = chinaMobile;
	}

	public MmochatPhoneCardType getPhoneCardType() {
		if (phone == null || phone.length() != 11) {
			return MmochatPhoneCardType.其它;
		}

		for (String str : ChinaUnicom) {
			if (phone.startsWith(str)) {
				return MmochatPhoneCardType.联通充值卡;
			}
		}
		for (String str : ChinaMobile) {
			if (phone.startsWith(str)) {
				return MmochatPhoneCardType.移动充值卡;
			}
		}
		return MmochatPhoneCardType.其它;
	}

	// 从当前时间开始，设置订单还能生存的时间
	public void setNewLifeTimeFromNow(long newTime) {
		lifeTime = System.currentTimeMillis() - createTime + newTime;
	}

	// 订单是否超时
	public boolean isTimeOut() {
		return System.currentTimeMillis() - createTime > lifeTime;
	}

	// 获取订单剩余时间
	public long getRestTime() {
		return Math.max(0, createTime + lifeTime - System.currentTimeMillis());
	}

	public long getLifeTime() {
		return lifeTime;
	}

	public void setLifeTime(long lifeTime) {
		this.lifeTime = lifeTime;
	}

	public String getSellRoleName() {
		return sellRoleName;
	}

	public void setSellRoleName(String sellRoleName) {
		this.sellRoleName = sellRoleName;
	}

	public String getBuyerName() {
		return buyerName;
	}

	public void setBuyerName(String buyerName) {
		this.buyerName = buyerName;
	}

	// 清除不完整的充值卡
	public void clearInvalidPhoneCard() {
		for (MmochatPhoneCard card : phoneCards.values()) {
			if (card.getAccount() == null || card.getPassword() == null) {
				phoneCards.remove(card.getId());
			}
		}
	}

	public long getBuyerSkyId() {
		return buyerSkyId;
	}

	public void setBuyerSkyId(long buyerSkyId) {
		this.buyerSkyId = buyerSkyId;
	}

	@Override
	public int compareTo(MmochatRoleDeal other) {
		return state.getPrority() - other.getState().getPrority();
	}
}
